package com.future.message.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:接口文档配置
 * @projectName:message
 * @see:com.future.message.configuration
 * @author:Xuyujie
 * @createTime:2021/12/3 21:33
 * @version:1.0
 */
@Configuration  //spring的配置文件
@EnableSwagger2  //开启swagger2
public class Swagger {

    @Bean
    public Docket docket() {
        ParameterBuilder builder=new ParameterBuilder();
        List<Parameter> list=new ArrayList<>();
        builder.name("token")
                .description("令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        list.add(builder.build());
        //指定版本
        return new Docket(DocumentationType.SWAGGER_2)
                //生成api的详细信息
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .groupName("future")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.future.message.Controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(list);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("FutureAPI")
                .description("Future的controller")
                .version("1.0")
                .contact(new Contact(
                        "future",
                        "http://localhost:9090/swagger-ui.html",
                        "154448675@qq.com"
                ))
                .build();
    }

}
